<?php
// Codenova.pl
// -------------------------
//
// Project: Jasminum
// Filename: AdminController.php
// Author: Kamil Mrzyglod
//
// ------------------------- 
// Copyright(c) 2013 by Codenova.pl


namespace Modules\JasminumCalendarReservations\Controllers;

use Gotcha\App\Extensions\GotchaCulturesExtension;
use Gotcha\App\Extensions\GotchaErrorsExtension;
use Gotcha\App\Extensions\GotchaLogsExtension;
use Gotcha\App\Extensions\HttpExtension;
use Gotcha\App\Framework\Controller;
use Gotcha\App\Types\String;
use GotchaDatabase\JasminumCalendarReservationsQuery;

class AdminController extends Controller {
    public function returnReservations() {
        $reservations = JasminumCalendarReservationsQuery::create()
            ->orderById()
            ->find();

        // HTML cache
        $html = '';
        foreach($reservations as $reservation) {
            $hall = array_search($reservation->getHall(), $this->getHalls());

            $html .= '<tr>';
            $html .= sprintf('<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>[ <a href="%s?id=%s">Edytuj</a> | <a href="%s?id=%s" class="delete">Usuń</a> ]</td>', $reservation->getFirstname().' '.$reservation->getLastname(), $reservation->getEmail(), $reservation->getDateFrom(), $reservation->getDateTo(), $reservation->getPhone(), $hall, parent::UrlContent('/Admin/JasminumCalendarReservations/Edit'), $reservation->getId(), parent::UrlContent('/Admin/JasminumCalendarReservations/Delete'), $reservation->getId());
            $html .= '</tr>';
        }

        if($reservations->count() == 0) {
            $html .= '<tr><td colspan="8">Nie ma dodanych rezerwacji.</td></tr>';
        }

        return $html;
    }
    public function EditPost() {
        $http = new HttpExtension();

        // Validate input
        if(empty($_POST['Id']) || !isset($_POST['Id'])) {
            die('Error');
        }

        if(empty($_POST['Firstname']) || empty($_POST['Lastname']) || empty($_POST['Email']) || empty($_POST['Phone']) || empty($_POST['DateFrom']) || empty($_POST['DateTo']) || empty($_POST['HallType'])) {
            header('Location: '.$http->resolveAbsoluteUrl().parent::UrlContent('/Admin/JasminumCalendarReservations/Edit?id='.$_POST['Id'].'&error=1'));
            exit;
        }

        // Update
        try {
            $reservation = JasminumCalendarReservationsQuery::create()
                ->findOneById($_POST['Id'])
                ->setFirstname($_POST['Firstname'])
                ->setLastname($_POST['Lastname'])
                ->setEmail($_POST['Email'])
                ->setPhone($_POST['Phone'])
                ->setDateFrom($_POST['DateFrom'])
                ->setDateTo($_POST['DateTo'])
                ->setHall($_POST['HallType'])
                ->save();
        } catch (\Exception $e) {
            GotchaLogsExtension::createNewLogEntry(new String($e->getMessage()));
            GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('ERROR_DURING_EXECUTING_QUERY'))));
            exit;
        }

        header('Location: '.$http->resolveAbsoluteUrl().parent::UrlContent('/Admin/Jasminum/Reservations?edit=2'));
        exit;
    }

    public function Delete() {
        $http = new HttpExtension();

        // Validate input
        if(empty($_GET['id']) || !isset($_GET['id'])) {
            header('Location: '.$http->resolveAbsoluteUrl().parent::UrlContent('/Admin/Jasminum/Reservations?error=2'));
            exit;
        }

        // Delete element
        try {
            JasminumCalendarReservationsQuery::create()->findOneById($_GET['id'])->delete();
        } catch (\Exception $e) {
            GotchaLogsExtension::createNewLogEntry(new String($e->getMessage()));
            GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('ERROR_DURING_EXECUTING_QUERY'))));
            exit;
        }

        header('Location: '.$http->resolveAbsoluteUrl().parent::UrlContent('/Admin/Jasminum/Reservations?delete=2'));
        exit;
    }


    public function getHalls() {
        $roomTypes = new \ReflectionClass('Modules\JasminumCalendarReservations\Enums\Halls');
        return $roomTypes->getConstants();
    }

    public function Edit() {
        return parent::View();
    }

    public function returnReservation($id) {
        $reservations = JasminumCalendarReservationsQuery::create()
            ->findOneById($id);
        return $reservations;
    }
}